package com.sj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sj.common.lang.ApiResultMsg;
import com.sj.entity.Blog;
import com.sj.entity.Tag;
import com.sj.service.BlogService;
import com.sj.service.TagService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 标签模块前端控制器
 * </p>
 *
 * @author 小七
 * @since 2021-07-30
 */
@RestController
@RequestMapping("/api/tag")
@Api(tags = "标签模块")
public class TagController extends BaseController {

    @Autowired
    private TagService tagService;

    @ApiOperation(value = "添加标签接口")
    @PutMapping("/save")
    public ApiResultMsg<Tag> save(@RequestBody @Validated Tag tag) {
        boolean result = tagService.save(tag);
        if (result) {
            return ApiResultMsg.success("添加成功");
        }
        return ApiResultMsg.error("添加失败");
    }

    @ApiOperation(value = "删除标签接口", notes = "会先判断该标签下是否有博客，没有才会删除")
    @DeleteMapping("/delete/{userId}/{id}")
    public ApiResultMsg<Tag> delete(@PathVariable("userId") @ApiParam(name = "userId", value = "用户id", required = true, defaultValue = "1") Integer userId,
                                    @PathVariable("id") @ApiParam(name = "id", value = "标签id", required = true, defaultValue = "1") Integer id) {
        // 先插询该标签是否有博客
        Integer blogCount = tagService.getCountTagBindBlog(id);
        if (blogCount > 0) {
            return ApiResultMsg.error("当前标签有博客不能删除");
        }
        boolean result = tagService.remove(new QueryWrapper<Tag>().eq("id", id).eq("user_id", userId));
        if (result) {
            return ApiResultMsg.success("删除成功");
        }
        return ApiResultMsg.error("删除失败");
    }

    @ApiOperation(value = "更新标签接口")
    @PostMapping("/update")
    public ApiResultMsg<Tag> update(@RequestBody @Validated Tag tag) {
        boolean result = tagService.updateById(tag);
        if (result) {
            return ApiResultMsg.success("更新成功");
        }
        return ApiResultMsg.error("更新失败");
    }

    @GetMapping("/list/{userId}/{current}/{size}")
    @ApiOperation(value = "查询用户所有标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "current", value = "当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示条数", required = true, defaultValue = "5")
    })
    public ApiResultMsg<Tag> listTagByUser(@PathVariable(value = "userId") Integer userId, @PathVariable(value = "current") Integer current, @PathVariable(value = "size") Integer size) {
        IPage<Tag> tagPageInfo = tagService.listTagByPage(userId, current, size);
        return ApiResultMsg.success(tagPageInfo);
    }

    @GetMapping("/list/{current}/{size}")
    @ApiOperation(value = "查询所有标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示条数", required = true, defaultValue = "5")
    })
    public ApiResultMsg<Tag> listTag(@PathVariable(value = "current") Integer current, @PathVariable(value = "size") Integer size) {
        IPage<Tag> tagPageInfo = tagService.listTagByPage(null, current, size);
        return ApiResultMsg.success(tagPageInfo);
    }

}

